﻿
using DataServer.Entities;
using IsPay.Logic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IsPay
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnsubmit_Click(object sender, EventArgs e)
        {
            String strvrifycode = Session["VARIFYCODE"].ToString();
            if (this.txtcheckcode.Text.Trim().Length <= 0)
            {
                this.lsbmsg.Text = "请输入验证码!!";
                return;
            }else
            {
                if (!strvrifycode.ToUpper().Equals(txtcheckcode.Text.Trim().ToUpper()))
                {
                    this.lsbmsg.Text = "验证码输入错误!!";
                    return;
                }
            }
            if (this.txtUserid.Text.Trim().Length <= 0)
            {
                this.lsbmsg.Text = "请输入用户名!!";
                return;
            }
            if (this.txtPwd.Text.Trim().Length <= 0)
            {
                this.lsbmsg.Text = "请输入密码!!";
                return;
            }
           
            String struseid = this.txtUserid.Text.Trim();
            //byte[] result = Encoding.Default.GetBytes(this.txtPwd.Text.Trim());
            //MD5 md5 = new MD5CryptoServiceProvider();
            //byte[] output = md5.ComputeHash(result);
            //String strpwd = BitConverter.ToString(output).Replace("-", "");
            String strpwd = CodeMange.getMD5StringCode(this.txtPwd.Text.Trim());
            UserSer lc = new UserSer();
            UserProfile user = lc.CheckUser(struseid, strpwd);
            if (user == null)
            {
                this.lsbmsg.Text = "用户名或密码错误!!";
            }
            else
            {
                UserHistory userhis = new UserHistory();
                userhis.UserId = user.UserId;
                userhis.UserName = user.UserName;
                userhis.Telephone = user.Telephone;
                userhis.IsActive = user.IsActive;
                userhis.Email = user.Email;
                userhis.AuthGroup = user.AuthGroup;
                userhis.LastPcIp = Request.UserHostAddress;
                userhis.LastPcMac = "";
                userhis.LastPCName = Request.UserHostName;
                userhis.LastLoginTimeKey = DateTime.Now.ToString("yyyyMMddHHmmss")+DateTime.Now.Millisecond.ToString();
                lc.AddUserLoginHis(userhis);
                MenuSer ms = new MenuSer();
                IEnumerable<DataServer.Entities.Menu> lsmenu = ms.GetMenusByFather(user.UserId, "");
                List<ShowMenu> lsShowMenu = ms.GetShowMenu(lsmenu);
                Session.Add("LOGINUSER", user);
                Session.Add("USERID", user.UserId);
                Session.Add("USERPHONE", user.Telephone);
                Session.Add("USERMAIL", user.Email);
                Session.Add("MENUS", lsShowMenu);
                Response.Redirect("MainFrame.aspx");
            }
        }
        
    }
}